package gone;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class OS extends Thread
{
	static String OSname;
	static String tasks[];
	static int nop;
	public static void gui()
	{
		tasks=new String[100];
		for(int i=0;i<100;i++)
			tasks[i]="";
		final JFrame frame=new JFrame();

		frame.setTitle("Task Manager v1.0  "+OSname);
		frame.setPreferredSize(new Dimension(450, 450));
		//frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setLayout(new BorderLayout());
				
		JMenuBar menubar=new JMenuBar();
		
		JMenu file=new JMenu("File");
		JMenu shutdown=new JMenu("Shutdown");
		JMenu help=new JMenu("Help");
		
		//File Menu
		
		JMenuItem mitem1= new JMenuItem("New Task");
		JMenuItem mitem3= new JMenuItem("Memory Details");
		JMenuItem mitem2= new JMenuItem("Exit");
		file.add(mitem1);
		file.add(mitem3);
		file.add(mitem2);
		class exitaction implements ActionListener{
			public void actionPerformed(ActionEvent e){
				frame.dispose();
				System.exit(0);
				
			}
		}
		mitem2.addActionListener(new exitaction());

		class newtask implements ActionListener{
			public void actionPerformed(ActionEvent e){
				newtask();
				
			}
		}
		mitem1.addActionListener(new newtask());
		
		class mem_details implements ActionListener{
			public void actionPerformed(ActionEvent e){
				mem_details();
				
			}
		}
		mitem3.addActionListener(new mem_details());
		//Shut Down Menu
		
		mitem1= new JMenuItem("Log Off");
		mitem2= new JMenuItem("Restart");
		mitem3= new JMenuItem("Shut Down");
		shutdown.add(mitem1);
		shutdown.add(mitem2);
		shutdown.add(mitem3);
		
		class logoff implements ActionListener{
			public void actionPerformed(ActionEvent e){
				try {
					logoff();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
			}
		}
		class restart implements ActionListener{
			public void actionPerformed(ActionEvent e){
				try {
					restart();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
			}
		}
		class shutdown implements ActionListener{
			public void actionPerformed(ActionEvent e){
				try {
					shutdown();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
			}
		}
		mitem1.addActionListener(new logoff());
		mitem2.addActionListener(new restart());
		mitem3.addActionListener(new shutdown());
		
		mitem1= new JMenuItem("Help");
		help.add(mitem1);
		class help implements ActionListener{
			public void actionPerformed(ActionEvent e){
				help();

			}
		}
		mitem1.addActionListener(new help());
		
		
		//Adding Objects to menu
		
		menubar.add(file);
		menubar.add(shutdown);
		menubar.add(help);
		frame.add(menubar,BorderLayout.PAGE_START);
		//Printing Processes
		pro_details();
		JPanel panel = new JPanel();
		JScrollPane spane ;
		JTextArea area;
		area = new JTextArea(tasks[1], 23, 35);
		for(int i=2;i<nop;i++)
		{
		area.append("\n"+tasks[i]);	
		}
		area.append("\n");
		spane = new JScrollPane(area);
	    panel.add(spane);
		frame.add(panel,BorderLayout.LINE_START);
		JButton button = new JButton("Kill process");
		
		
		ActionListener actionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		  		killtask();
		      }
		    };
		button.addActionListener(actionListener);
		frame.add(button,BorderLayout.PAGE_END);
		frame.pack();
	}

	
	
	public static void newtask()
	{
		String taskname;
		taskname = JOptionPane.showInputDialog("Enter the process you want to execute: ");
		try
		{
			Runtime.getRuntime().exec(taskname);
		}
		catch(IOException ex)
		{
			JOptionPane.showMessageDialog(null,"Error");
		}
	}
	
	public static void killtask()
	{
		try
  		{
  			String taskname = JOptionPane.showInputDialog("Enter the process you want to terminate: ");
  			if(OSname=="Windows")
  	        {
  			Runtime.getRuntime().exec("tskill "+taskname);
  	        }
  			else
  			{
  				Runtime.getRuntime().exec("killall "+taskname);
  			}
  		}
  		catch(IOException ex)
  		{
  			JOptionPane.showMessageDialog(null,"Error");
  		}
		
	}
	
	public static void shutdown() throws Exception
	{
		JOptionPane.showMessageDialog(null,"Shuting Down");
		if(OSname=="Windows")
        {
		Runtime.getRuntime().exec("Shutdown -s");
        }
		else
		{
			Runtime.getRuntime().exec("sudo shutdown -h now");
		}
	}
	public static void restart() throws Exception
	{
		JOptionPane.showMessageDialog(null,"Restart");
		if(OSname=="Windows")
        {
		Runtime.getRuntime().exec("Shutdown -r");
        }
		else
		{
			Runtime.getRuntime().exec("sudo shutdown -r now");
		}
	}
	public static void logoff() throws Exception
	{
		JOptionPane.showMessageDialog(null,"Logging Off");
		if(OSname=="Windows")
        {
		Runtime.getRuntime().exec("Shutdown -l");
        }
		else
		{
		Runtime.getRuntime().exec("sudo halt now -h");
		}
	}
	
	public static void help()
	{
		JOptionPane.showMessageDialog(null,"Task Manager v1.0"+"\nBy:  Chirag Gupta and Rohit Dixit");
	}
	
	public static void mem_details() { 
		JFrame mem_frame=new JFrame();

		mem_frame.setTitle("Memory Details");
		mem_frame.setPreferredSize(new Dimension(350, 350));
		mem_frame.setLocationRelativeTo(null);
		//mem_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mem_frame.setVisible(true);
		mem_frame.setLayout(new BorderLayout());
		mem_frame.pack();
	    
	    File[] roots = File.listRoots(); 
	 
	    JPanel mem_panel = new JPanel();
		JScrollPane mem_spane ;
		JTextArea mem_area;
		mem_area = new JTextArea("", 19, 25);
	    for (File root : roots) { 
	    	mem_area.append("\nFile system root:      " + root.getAbsolutePath()); 
	    	mem_area.append("\nTotal space (bytes):   " + root.getTotalSpace()); 
	    	mem_area.append("\nFree space (bytes):    " + root.getFreeSpace()); 
	    	mem_area.append("\nUsable space (bytes):  " + root.getUsableSpace()); 
	    } 

		mem_spane = new JScrollPane(mem_area);
		mem_panel.add(mem_spane);
		mem_frame.add(mem_panel,BorderLayout.PAGE_START);
		mem_frame.pack();
	  } 
	
	public static void pro_details() { 
		try { 
			int i=0;
	        String line; 
	        Process p;
	        if(OSname=="Windows")
	        {
	        p = Runtime.getRuntime().exec("tasklist.exe"); 
	        }
	        else
	        {
	        	p = Runtime.getRuntime().exec("top");
	        }
	        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
	        while ((line = input.readLine()) != null) { 
	            tasks[i]=line;
	            i++;
	        } 
	        nop=i;
	        input.close(); 
	    } catch (Exception err) { 
	        err.printStackTrace(); 
	    } 

	}
	
	public static void main(String[] args) throws Exception
	{
		String nameOS = "os.name";
		String osname=System.getProperty(nameOS);
		
        if(osname.startsWith("Windows"))
        {
        	OSname="Windows";
        }
        else 
        {
        	OSname="Linux";
        }

        gui();
	}
}
